radiusNow = 300;
radius1hr = 225;
radius3hr = 150;
radius6hr = 75;
radius12hr = 0;

fakeIndex = 0;
fakeIndex2 = 0; 


/*
 * private.
 */
getCurrentTime = function(){
	var currentTime = new Date();
	return currentTime; 
};

/*
 * private: checks to see if theDate is in between the firstTime and secondTime. 
 * returns a boolean.
 */
checkBetweenTimes = function(theDate, firstTime, secondTime){
	if(theDate >= firstTime && theDate < secondTime)
		return true;
	else 
		return false;

};

/*
 * Takes a Date, a numHours and numMinutes to add.
 * Returns a new Date object with the updated time.
 */
addHoursAndMinutes = function(theDate, numHours, numMinutes){
	//add hours and minutes
	var newMinutes = theDate.getMinutes() + numMinutes;
	var newHours = theDate.getHours() + numHours;
	var newDays = theDate.getDate(); 
	
	
	//rotate back
	if(newMinutes > 59){
		newMinutes = newMinutes - 60;
		newHours = 1 + newHours;
	}
	if(newHours > 23){
		newHour = newHours - 24;
		newDays = 1 + newDays; 
	}

	//set the new date.
	var newDate = new Date(theDate.getFullYear(),
			theDate.getMonth(), 
			newDays, 
			newHours, 
			newMinutes, 
			theDate.getSeconds(), 
			theDate.getMilliseconds());
	
	return newDate;
}; 


/*
 * private. Small helper function. 
 */
calculateRadius = function(hourToCheck, lowerHour, upperHour, lowerPlace, upperPlace){
	var denominator = lowerHour - upperHour;
	var numerator = (upperHour*60 - hourToCheck)/60;
	var distanceBetweenHashLines = upperPlace - lowerPlace;
	var returnRadius = upperPlace + (distanceBetweenHashLines * numerator / denominator);
	return returnRadius;
	
};

/*
 * Output the radius from the center (12-hour mark), given a specific time.
 */
getRadius = function(endTime){
	var radius = 0;
	var currentTime = getCurrentTime();
	if(endTime < currentTime){
		radius = 315;
	}
	else if(checkBetweenTimes(endTime, addHoursAndMinutes(currentTime, 0, 0), addHoursAndMinutes(currentTime, 1, 0)) == true){
		console.log("Between 0 and 1"); 
		var hourToCheck = endTime.getHours() - currentTime.getHours();
		if(hourToCheck < 0)
			hourToCheck = hourToCheck + 24;
		var minuteToCheck = endTime.getMinutes() - currentTime.getMinutes();
		if(minuteToCheck < 0)
			minuteToCheck = minuteToCheck + 60; 
		var timeToCheck = 60 * hourToCheck + minuteToCheck; 
		radius = calculateRadius(timeToCheck, 1, 0, radius1hr, radiusNow);
	}
	else if(checkBetweenTimes(endTime, addHoursAndMinutes(currentTime, 1, 0), addHoursAndMinutes(currentTime, 3, 0)) == true){
		console.log("Between 1 and 3"); 
		var hourToCheck = endTime.getHours() - currentTime.getHours();
		if(hourToCheck < 0)
			hourToCheck = hourToCheck + 24;
		var minuteToCheck = endTime.getMinutes() - currentTime.getMinutes();
		if(minuteToCheck < 0)
			minuteToCheck = minuteToCheck + 60; 		
		var timeToCheck = 60 * hourToCheck + minuteToCheck; 	
		radius = calculateRadius(timeToCheck, 3, 1, radius3hr, radius1hr);

	}
	else if(checkBetweenTimes(endTime, addHoursAndMinutes(currentTime, 3, 0), addHoursAndMinutes(currentTime, 6, 0)) == true){
		console.log("Between 3 and 6"); 
		var hourToCheck = endTime.getHours() - currentTime.getHours();
		if(hourToCheck < 0)
			hourToCheck = hourToCheck + 24;
		var minuteToCheck = endTime.getMinutes() - currentTime.getMinutes();
		if(minuteToCheck < 0)
			minuteToCheck = minuteToCheck + 60; 		
		var timeToCheck = 60 * hourToCheck + minuteToCheck; 
		radius = calculateRadius(timeToCheck, 6, 3, radius6hr, radius3hr);

	}
	else if(checkBetweenTimes(endTime, addHoursAndMinutes(currentTime, 6, 0), addHoursAndMinutes(currentTime, 12, 0)) == true){
		console.log("Between 6 and 12"); 
		var hourToCheck = endTime.getHours() - currentTime.getHours();
		if(hourToCheck < 0)
			hourToCheck = hourToCheck + 24;
		var minuteToCheck = endTime.getMinutes() - currentTime.getMinutes();
		if(minuteToCheck < 0)
			minuteToCheck = minuteToCheck + 60; 		
		var timeToCheck = 60 * hourToCheck + minuteToCheck; 
		radius = calculateRadius(timeToCheck, 12, 6, radius12hr, radius6hr);

	}
	else if(endTime > addHoursAndMinutes(currentTime, 12)){
		console.log("Greater than 12"); 
	}
	else
		alert("Something broke on Jay's part..."); 
	return radius;
};


/*
 * This needs to be filled in from Sachin.
 */
getEvents = function(){
	
	return [];
};


/*
 * Sachin needs to fill this in.
 * Given an event object from the array, the event Name is returned.
 */
getEventName = function(eventObject){
	if(fakeIndex == 0)
		return "Macs 100";
	return "Insanity Power and Balance"; 
};

/*
 * Sachin
 */
getEventLocation = function(eventObject){
	if(fakeIndex == 0)
		return "Natural History Building";
	return "The Apartment";
};

/*
 * Sachin
 */
getEventStartTime = function(eventObject){
	if(fakeIndex == 0)
		return (new Date("March 7, 2012 18:00:00"));
	return (new Date("March 7, 2012 20:15:00"));
};

/*
 * Sachin
 */
getEventEndTime = function(eventObject){
	if(fakeIndex == 0)
		return (new Date("March 7, 2012 20:00:00"));
	return (new Date("March 7, 2012 21:00:00"));
};



/*
 * Sean's JSON Function: Creates an object of the form
 * {
   "circles":[
      {
         "name":"Macs 100",
         "location":"Natural History Building",
         "rI":"116.25",
         "rR":"58.125"
      },
      {
         "name":"Insanity Power and Balance",
         "location":"The Apartment",
         "rI":"91.25",
         "rR":"18.75"
      }
   ]
}
 */
generateOuterCircles = function(){
	
	var eventArray = getEvents();
	eventArray.length = 2; 
	
	var circleObject = {};
	circleObject.circles = []; 

	for(var i = 0; i < eventArray.length; i++, fakeIndex++){
		var thisEvent = eventArray[i]; 
		circleObject.circles[i] = {};
		circleObject.circles[i].name = getEventName(thisEvent);
		circleObject.circles[i].location = getEventLocation(thisEvent);
		
		//get inner radius
		var endDate = getEventEndTime(thisEvent);
		var innerRadius = getRadius(endDate); 
		
		var startDate = getEventStartTime(thisEvent);
		var outerRadius = getRadius(startDate);
		
		var ringRadius = outerRadius - innerRadius;
		
		circleObject.circles[i].rI = "" + innerRadius;
		circleObject.circles[i].rR = "" + ringRadius;
		
	}
	
	return circleObject; 
	
};

/*
 * Sean's other JSON function. 
 * Creates an object of the form.... 
	{
	   "jsonAdd":[
	      {
	         "type":"circle",
	         "cx":350,
	         "cy":350,
	         "opacity":1,
	         "r":315,
	         "fill":"#ff0000"
	      },
	      {
	         "type":"circle",
	         "cx":350,
	         "cy":350,
	         "opacity":1,
	         "r":187.5,
	         "fill":"#ff8080"
	      }
	   ]
	}
*/
generateInnerCircles = function(){
	var circleObject = {};
	circleObject.jsonAdd = [];
	
	var eventArray = getEvents();
	eventArray.length = 2;
	
	for(var i = 0; i < eventArray.length; i++, fakeIndex++){
		circleObject.jsonAdd[i] = {};

		//initialize invariats.
		circleObject.jsonAdd[i].type = "circle";
		circleObject.jsonAdd[i].cx = 350;
		circleObject.jsonAdd[i].cy = 350;
		circleObject.jsonAdd[i].opacity = 1;
		
		var thisEvent = eventArray[i];
		var startDate = getEventStartTime(thisEvent);
		var outerRadius = getRadius(startDate);
		
		circleObject.jsonAdd[i].r = outerRadius;
		
		
		//do the colors.
		var hue = 0;
		var value = 1;
		var saturation = 1 - (i/eventArray.length);
		
		var rgbObject = hsv2rgb(hue, saturation, value);
		var toHexString = "rgb(" + rgbObject.red + ", " + rgbObject.green + ", " + rgbObject.blue + ")";
		var hexColor = colorToHex(toHexString);

		circleObject.jsonAdd[i].fill = "" + hexColor;
	}
	
	console.log(JSON.stringify(circleObject));  
	return circleObject;

}(); 



